/*
 * Copyright 2012-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package sample.web.thymeleaf3;

import org.beetl.core.GroupTemplate;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.beetl.ext.spring.BeetlSpringViewResolver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ViewResolver;
import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;

import java.util.LinkedList;
import java.util.List;

@Configuration
@SpringBootApplication
@Controller
public class SampleWebThymeleaf3Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleWebThymeleaf3Application.class, args);
    }

    private Customer customer = new Customer("low","beetl");

    @GetMapping("/null")
    public String nullTest(Model model) {
        model.addAttribute("customer",customer);
        return "/templates/null.html";
    }

    @GetMapping("/layout")
    public String layoutTest(Model model) {
        List<Customer> users = new LinkedList<Customer>();
        users.add(customer);
        users.add(new Customer("good","thymeleaf"));
        users.add(new Customer("good","freemarker"));
        users.add(new Customer("good","JSP"));
        model.addAttribute("users",users);
        return "layout";
    }

    @Bean
    public ViewResolver beetlSpringViewResolver() {
        BeetlSpringViewResolver viewResolver = new BeetlSpringViewResolver();
        BeetlGroupUtilConfiguration beetConfig = new BeetlGroupUtilConfiguration();
        ClasspathResourceLoader classpathResourceLoader = new ClasspathResourceLoader("/templates/");
        beetConfig.setResourceLoader(classpathResourceLoader);
        beetConfig.setSuffix(".html");
        viewResolver.setViewNames("/templates/null.html");
        viewResolver.setConfig(beetConfig);
        viewResolver.setOrder(0);
        viewResolver.setGroupTemplate(new GroupTemplate());
        viewResolver.setContentType("text/html;charset=utf-8");
        return viewResolver;
    }

    @Bean
    public SpringResourceTemplateResolver defaultTemplateResolver(ApplicationContext applicationContext) {
        SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        resolver.setApplicationContext(applicationContext);
        resolver.setPrefix("classpath:/templates/");
        resolver.setSuffix(".html");
        resolver.setTemplateMode("HTML");
        resolver.setCacheable(true);
        resolver.setUseDecoupledLogic(true);
        resolver.setOrder(1);
        return resolver;
    }
}

class Customer{

    private String type;
    private String nickname;

    public Customer(String type, String nickname) {
        this.type = type;
        this.nickname = nickname;
    }

    public String getType() {
        return type;
    }

    public Customer setType(String type) {
        this.type = type;
        return this;
    }

    public String getNickname() {
        return nickname;
    }

    public Customer setNickname(String nickname) {
        this.nickname = nickname;
        return this;
    }
}
